package lambda.function;

import lambda.entity.User;

import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author LIBOFEI
 * @ClassName PredicateTest
 * @description Predicate<T>  断言型接口 对类型为 T 的对象进行操作返回 R 类型的对象
 * @date 2022/4/6 17:07
 * @Version 1.0
 **/
public class PredicateTest {

    private static int num = -1;

    public static void main(String[] args) {
        //未使用lamda表达式之前
        boolean b = testUser(new Supplier<User>() {
                                 @Override
                                 public User get() {
                                     return getUser();
                                 }
                             },
                 10,
                        new Predicate<User>() {
                            @Override
                            public boolean test(User user) {
                                return user.getId() > 10;
                            }
        });
        System.out.println(b);


        //使用lamda表达式
        boolean b2 = testUser(() -> getUser(), 10, user -> user.getId() > 10);
        System.out.println(b2);
    }

    public static User getUser(){
        User user = new User();
        user.setId(num);
        user.setName(""+num);
        ++num;
        return user;
    }


    public static boolean testUser(Supplier<User> userSupplier, int maxCount, Predicate<User> predicate){
        while (maxCount-- > 0){
            User user = userSupplier.get();
            boolean test = predicate.test(user);
            if(test){
                return true;
            }
        }
        return false;
    }
}
